package chinasoft;

import chinasoft.entity.VehicleRealTime;
import chinasoft.util.FormatUtil;
import chinasoft.util.JsonUtil;

import java.util.List;

/**
 * Created by kong on 2017/7/31.
 */
public class Test {

    //存放所有属性对象的List
    public static List<Attr> attrList;
    //json传串
    private static String str = null;
    //中间值
    private static String midValue = null;

    static {
        str = JsonUtil.ReadFile("data.json");

    }

    public static void main(String[] args) {

        String status = "7B7C00583236333536363833344C3131470000009400000000000003000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000BF7D7E";

        //清空
        if (attrList != null && attrList.size() > 0) {
            attrList.clear();
        }
        attrList = JsonUtil.changeGsonToList(str, Attr.class);
        VehicleRealTime vehicleRealTime = new VehicleRealTime();
        StringBuffer sb = new StringBuffer("{");  //RealTimeObj json字符串
        //1.给List里面所有属性赋值
        for (Attr at : attrList) {
            setListValue(at, status);
            sb.append("'" + at.getName() + "':'" + at.getValue() + "',");
        }
        //2.去掉最后一个,
        int lastIndex = sb.toString().lastIndexOf(",");
        StringBuffer packedSb = new StringBuffer(sb.toString().substring(0, lastIndex));
        packedSb.append("}");

        //省略步骤...一些特殊字段的取值
//        System.out.println(packedSb);
        //3.将List转为对象字符串
        vehicleRealTime = JsonUtil.changeGsonToBean(packedSb.toString(), VehicleRealTime.class);

        System.out.println(vehicleRealTime.getLat());

        /***初始化 StatisticData 对象**/
        /*StatisticData statisticData = new StatisticData();
        * AnalyzeData analyzeData = new AnalyzeData();
        * VehicleStatus vehicleStatus = new VehicleStatus();
        * VehicleRealTime vehicleRealTime = new VehicleRealTime();
        * */
    }

    /**
     * 给所有字段解析值
     *
     * @param at
     * @param status
     */
    private static void setListValue(Attr at, String status) {
        if (at.getStartBit() > 0 && at.getEndBit() > 0) {
            if (at.getName().equals("lng") || at.getName().equals("lat"))
                midValue = (Long.parseLong(status.substring(26, 34), 16) / 100000) + "";
            else
                midValue = FormatUtil.HexToIntStr(status.substring(at.getStartBit(), at.getEndBit()), 16);
        }
        at.setValue(midValue);
    }

    /**
     * 给VehicleRealtime对象赋值
     *
     * @param vehicleRealTime
     * @param attrList
     */
    private static void setVehicleRealtimeValue(VehicleRealTime vehicleRealTime, List<Attr> attrList) {
    }
}

